package com.gnerv.pabu.basic.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.pabu.basic.core.dto.ResetPasswordDTO;
import com.gnerv.pabu.basic.core.service.IUserService;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.dto.UserDTO;
import com.gnerv.pabu.basic.core.dto.UserRoleDTO;
import com.gnerv.pabu.basic.core.dto.UserSaveDTO;
import com.gnerv.pabu.basic.core.dto.UserSearchDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础平台-用户 controller 类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "用户管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/user")
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation("保存 用户")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveUser(@RequestBody UserSaveDTO userSaveDTO) {
        userService.saveUser(userSaveDTO);
        return new Result();
    }

    @ApiOperation("新建 用户")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createUser(@RequestBody UserSaveDTO userSaveDTO) {
        userService.createUser(userSaveDTO);
        return new Result();
    }

    @ApiOperation("删除 用户")
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    public Result deleteUser(@PathVariable("userId") String userId) {
        userService.deleteUser(userId);
        return new Result();
    }

    @ApiOperation("更新 用户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateUser(@RequestBody UserSaveDTO userSaveDTO) {
        userService.updateUser(userSaveDTO);
        return new Result();
    }

    @ApiOperation("更新 用户")
    @RequestMapping(value = "/{userId}/status/{status}", method = RequestMethod.GET)
    public Result updateUserStatus(@PathVariable("userId") String userId, @PathVariable("status") Integer status) {
        userService.updateUserStatus(userId, status);
        return new Result();
    }

    @ApiOperation("查询 用户 根据用户id")
    @RequestMapping(value = "/select/{userId}", method = RequestMethod.GET)
    public Result selectUser(@PathVariable("userId") String userId) {
        UserDTO user = userService.selectUser(userId);
        return new Result().data("user", user);
    }

    @ApiOperation("查询 用户 列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listUser(@RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "300") Integer size,
                           UserSearchDTO searchUser) {
        Page<UserDTO> page = userService.listUser(current, size, searchUser);
        return new Result().data("users", page);
    }

    @ApiOperation("绑定 用户角色")
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result userBindRole(@RequestBody UserRoleDTO userRoleDTO) {
        userService.bindUserRole(userRoleDTO.getUserId(), userRoleDTO.getRoleIds());
        return new Result();
    }

    @ApiOperation("查询 用户 列表 根据组织机构")
    @RequestMapping(value = "/list/org/{orgId}", method = RequestMethod.GET)
    public Result listUsersByOrgId(@PathVariable("orgId") String orgId) {
        List<UserDTO> users = userService.listUsersByOrgId(orgId);
        return new Result().data("users", users);
    }

    @ApiOperation("查询 用户 列表 根据角色")
    @RequestMapping(value = "/list/role/{roleId}", method = RequestMethod.GET)
    public Result listUsersByRoleId(@PathVariable("roleId") String roleId) {
        List<UserDTO> users = userService.listUsersByRoleId(roleId);
        return new Result().data("users", users);
    }

    @ApiOperation("重置 用户 密码")
    @RequestMapping(value = "/reset/password", method = RequestMethod.POST)
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        userService.resetPassword(resetPasswordDTO.getUserId(), resetPasswordDTO.getPasswordType(), resetPasswordDTO.getPassword());
        return new Result();
    }
}
